﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace XiaofangExamFramework
{
    public class Win32SendInput
    {
        [DllImport("user32.dll")]
        static extern void SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
        [StructLayout(LayoutKind.Sequential)]
        struct INPUT
        {
            public uint type;
            public InputUnion U;
            public static int Size => Marshal.SizeOf<INPUT>();
        }
        [StructLayout(LayoutKind.Explicit)]
        struct InputUnion
        {
            [FieldOffset(0)]
            public MOUSEINPUT mi;
            [FieldOffset(0)]
            public KEYBDINPUT ki;
        }
        [StructLayout(LayoutKind.Sequential)]
        struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
        [StructLayout(LayoutKind.Sequential)]
        struct KEYBDINPUT
        {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        private const int VK_CONTROL = 0x11;
        private const int VK_C = 0x43;
        public static void SendCtrlC()
        {
            var ctrlDown = new INPUT { type = 1 };
            ctrlDown.U.ki.wVk = (ushort)VK_CONTROL;

            var cDown = new INPUT { type = 1 };
            cDown.U.ki.wVk = (ushort)VK_C;

            var cUp = new INPUT { type = 1 };
            cUp.U.ki.wVk = (ushort)VK_C;
            cUp.U.ki.dwFlags = 2;

            var ctrlUp = new INPUT { type = 1 };
            ctrlUp.U.ki.wVk = (ushort)VK_CONTROL;
            ctrlUp.U.ki.dwFlags = 2;

            var inputs = new[] { ctrlDown, cDown, cUp, ctrlUp };
            SendInput((uint)inputs.Length, ref inputs[0], INPUT.Size);
        }
    }
}
